﻿ 
 
using SHKF.B2C.BLL.Center;
using SHKF.B2C.Model;
using SHKF.B2C.Model.Dto.Order;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Entity.LM;
using SHKF.B2C.Model.Entity.Order;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
 

namespace SHKF.B2C.BLL.Order
{
    public class OrderBLL : LMBLL
    { 
        private readonly ShopMapBLL shopBll;
         
        public OrderBLL(SqlSugarClient db,
            ShopMapBLL shopBll) :
            base(db)
        { 
            
            this.shopBll = shopBll; 
        }

        /// <summary>
        /// 更新定单状态
        /// </summary>
        /// <param name="app_poi_code"></param>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public int UpdateStatus(long app_poi_code, long order_id, int status)
        {
            
            OrderContentEntity order = this.db.Queryable<OrderContentEntity>()
                   .Where(oo => oo.app_poi_code == app_poi_code
                                 && oo.order_id == order_id)
                   .First();

            int iResult = 0;
            if (order != null)
            {
                order.status = status;
                iResult = db
                        .Updateable<OrderContentEntity>(order)  
                        .UpdateColumns(a=>a.status)
                        .ExecuteCommand();
            }
            return iResult;
        }

        public void SaveAll(OrderContentEntity orderEntity, List<OrderDetailEntity> orderDetails, List<OrderAccountEntity> orderAccounts)
        {
            try
            {
                this.db.BeginTran();

                //保存定单
                long SN = this.SaveOrder(orderEntity);

                //保存明细
                if (orderDetails != null)
                {
                    foreach (OrderDetailEntity orderDetail in orderDetails)
                    {
                        this.SaveDetail(orderDetail);
                    }
                }
                 
                //保存账目
                foreach(OrderAccountEntity account in orderAccounts)
                {
                    account.OrderSN = SN;
                    account.pDate = DateTime.Now;
                    OrderAccountEntity oldAccount = this.db
                         .Queryable<OrderAccountEntity>()
                         .Where(ww => ww.OrderSN == SN && ww.KeyWord == account.KeyWord)
                         .First();

                    account.pDate = DateTime.Now;
                    if (oldAccount == null)
                    { 
                        this.db
                            .Insertable<OrderAccountEntity>(account)
                            .ExecuteCommand();

                    }
                    else
                    {
                        account.SN = oldAccount.SN;
                        this.db
                            .Updateable<OrderAccountEntity>(account)
                            .ExecuteCommand();
                    }
                }
                 
                this.db.CommitTran();
 
            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }
           
        }

        protected long SaveOrder(OrderContentEntity order)
        {
            try
            {
                long SN = 0;
                OrderContentEntity orderOld = this.db.Queryable<OrderContentEntity>()
                    .Where(oo => oo.app_poi_code == order.app_poi_code
                                  && oo.order_id == order.order_id)
                    .First();

                if (orderOld != null)
                {
                    SN = orderOld.SN;
                    order.SN = SN;
                    int iResult = this.db
                        .Updateable<OrderContentEntity>(order) 
                        .ExecuteCommand();
                }
                else
                {
                    SN = this.GetOrderID();
                    order.SN = SN;
                    this.db.Insertable<OrderContentEntity>(order)
                        .ExecuteCommand();
                }
                return SN;
            }
            catch (Exception ex)
            {
                throw ex;
            } 
        }

        /// <summary>
        /// 保存收货人信息
        /// </summary>
        /// <param name="receiver"></param>
        /// <returns></returns>
        public long SaveReceiver(OrderReceiverEntity receiver)
        {
            try
            {
                long SN = 0;
                OrderReceiverEntity orderOld = db
                    .Queryable<OrderReceiverEntity>()
                    .Where(oo => oo.FKSN == receiver.FKSN)
                    .First();

                if (orderOld != null)
                {
                    SN = orderOld.SN;
                    receiver.SN = SN;
                    int iResult = db
                        .Updateable<OrderReceiverEntity>(receiver) 
                        .ExecuteCommand();
                }
                else
                { 
                   SN = db
                        .Insertable<OrderReceiverEntity>(receiver)
                        .ExecuteReturnBigIdentity();
                }
                return SN;
            }
            catch (Exception ex)
            {
                throw ex;
            } 
        }

        public List<OrderContentDto> Query(long shopSN, int status, DateTime create_start, DateTime create_end)
        {
            List<OrderContentDto> list = new List<OrderContentDto>();

            var cmdText = base.db
                .Queryable<OrderContentEntity>()
                .Where(ww => ww.ShopSN == shopSN)
                .Where(w => w.ctime >= create_start && w.ctime <= create_end)
                .Select(it => new OrderContentDto());

            if (status != 0)
            {
                cmdText.Where(w => w.status == status);
            }

            list = cmdText.ToList();

            return list;
        }

        public List<OrderDetailEntity> QueryDetail(long order_id)
        { 
            var freesql = db.Queryable<OrderDetailEntity>()
               .Where(w=>w.FKSN == order_id);
 
            return freesql.ToList();
        }

        public List<OrderAccountEntity> QueryAccounts(long order_id)
        {
            var cmd = db.Queryable<OrderAccountEntity>()
              .Where(w => w.OrderSN == order_id);

            return cmd.ToList();
        }

        /// <summary>
        /// 更新配送状态
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int UpdateLogisticsStatus(OrderContentEntity order)
        { 
            int iResult =  this.db.Updateable<OrderContentEntity>(order) 
                    .UpdateColumns(a=>new { a.logistics_status })
                    .ExecuteCommand(); 

            return iResult;
        }
         
        protected long SaveDetail(OrderDetailEntity orderDetail)
        {
            long SN = 0;
            OrderDetailEntity orderDetailEntity = db.Queryable<OrderDetailEntity>()
                .Where(oo => oo.FKSN == orderDetail.FKSN 
                    && oo.app_medicine_code == orderDetail.app_medicine_code
                    && oo.price == orderDetail.price)
                .First();
            if (orderDetailEntity != null)
            {
                orderDetail.SN = orderDetailEntity.SN;
                int iResult = db.Updateable<OrderDetailEntity>(orderDetail) 
                   .ExecuteCommand(); 
            }
            else
            {
                SN = db.Insertable<OrderDetailEntity>(orderDetail)
                      .ExecuteReturnBigIdentity();
            }

            return SN;
        }
         
        public long GetOrderID()
        {
            OrderID order = new OrderID();
            order.CreateDate = DateTime.Now;
 
            long id = db
                .Insertable<OrderID>(order)
                .ExecuteReturnBigIdentity();

            return id;
        }

        /// <summary>
        /// 根据定单号获取平台
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public Order2 GetPlatform(long order_id) 
        {
             
            Order2 order = this.db
                .SqlQueryable<Order2>("select * from v_order_plat") 
                .Where(oo=>oo.SN == order_id)
                .First();

            return order;
        }

          
    }
}
